package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.AreaEntity;
import com.ly.heart.domain.KingBaseTaxRateSystemEntity;
import com.ly.heart.mapper.KingBaseTaxRateSystemMapper;
import com.ly.heart.service.IAreaService;
import com.ly.heart.service.IKingBaseTaxRateSystemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 0:42 2024-08-20
 * @return
 */
@Service
public class KingBaseTaxRateSystemServiceImpl extends ServiceImpl<KingBaseTaxRateSystemMapper, KingBaseTaxRateSystemEntity> implements IKingBaseTaxRateSystemService {

    @Resource
    private KingBaseTaxRateSystemMapper kingBaseTaxRateSystemMapper;
    @Resource
    private IAreaService iAreaService;

    @Override
    public AjaxResult getKingBaseTaxRateSystemList(KingBaseTaxRateSystemEntity kingBaseTaxRateSystemEntity){
        List<KingBaseTaxRateSystemEntity> list = kingBaseTaxRateSystemMapper.selectKingBaseTaxRateSystemEntityList(kingBaseTaxRateSystemEntity);
        list.forEach ( rate -> {
            AreaEntity areaEntity = iAreaService.getById ( rate.getAreaId () );
            if ( areaEntity != null ) {
                rate.setAreaName ( areaEntity.getAreaName () );
            }
        } );
        Page<KingBaseTaxRateSystemEntity> page = new Page<>(kingBaseTaxRateSystemEntity.getPage(),kingBaseTaxRateSystemEntity.getLimit());
        return AjaxResult.success(ReturnPage.getReturnPage(page,list));
    }

    /**
     * 查询金蝶-税率-税收制度
     *
     * @param id 金蝶-税率-税收制度ID
     * @return 金蝶-税率-税收制度
     */
    @Override
    public KingBaseTaxRateSystemEntity selectKingBaseTaxRateSystemEntityById(String id)
    {
        return kingBaseTaxRateSystemMapper.selectKingBaseTaxRateSystemEntityById(id);
    }

    /**
     * 查询金蝶-税率-税收制度列表
     *
     * @param kingBaseTaxRateSystemEntity 金蝶-税率-税收制度
     * @return 金蝶-税率-税收制度
     */
    @Override
    public List<KingBaseTaxRateSystemEntity> selectKingBaseTaxRateSystemEntityList(KingBaseTaxRateSystemEntity kingBaseTaxRateSystemEntity)
    {
        return kingBaseTaxRateSystemMapper.selectKingBaseTaxRateSystemEntityList(kingBaseTaxRateSystemEntity);
    }

    /**
     * 新增金蝶-税率-税收制度
     *
     * @param kingBaseTaxRateSystemEntity 金蝶-税率-税收制度
     * @return 结果
     */
    @Override
    public int insertKingBaseTaxRateSystemEntity(KingBaseTaxRateSystemEntity kingBaseTaxRateSystemEntity)
    {
        kingBaseTaxRateSystemEntity.setId(SnowflakeIdWorker.getInstance().nextStringId());
        kingBaseTaxRateSystemEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseTaxRateSystemEntity.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.isEmpty(kingBaseTaxRateSystemEntity.getDocNo())){
            int count = this.count ();
            kingBaseTaxRateSystemEntity.setDocNo("SSZD".concat ( String.format ( "%08d", count ) ) );
        }
        return kingBaseTaxRateSystemMapper.insertKingBaseTaxRateSystemEntity(kingBaseTaxRateSystemEntity);
    }

    /**
     * 修改金蝶-税率-税收制度
     *
     * @param kingBaseTaxRateSystemEntity 金蝶-税率-税收制度
     * @return 结果
     */
    @Override
    public int updateKingBaseTaxRateSystemEntity(KingBaseTaxRateSystemEntity kingBaseTaxRateSystemEntity)
    {
        kingBaseTaxRateSystemEntity.setUpdateBy(SecurityUtils.getLoginUser ().getUser ().getUserId ());
        kingBaseTaxRateSystemEntity.setUpdateTime(DateUtils.getNowDate());
        return kingBaseTaxRateSystemMapper.updateKingBaseTaxRateSystemEntity(kingBaseTaxRateSystemEntity);
    }

    /**
     * 批量删除金蝶-税率-税收制度
     *
     * @param ids 需要删除的金蝶-税率-税收制度ID
     * @return 结果
     */
    @Override
    public int deleteKingBaseTaxRateSystemEntityByIds(String[] ids)
    {
        return kingBaseTaxRateSystemMapper.deleteKingBaseTaxRateSystemEntityByIds(ids);
    }

    /**
     * 删除金蝶-税率-税收制度信息
     *
     * @param id 金蝶-税率-税收制度ID
     * @return 结果
     */
    @Override
    public int deleteKingBaseTaxRateSystemEntityById(String id)
    {
        return kingBaseTaxRateSystemMapper.deleteKingBaseTaxRateSystemEntityById(id);
    }

    @Override
    public AjaxResult disabled(String id) {
        KingBaseTaxRateSystemEntity kingBaseTaxRateSystemEntity = this.getById(id);
        if ( kingBaseTaxRateSystemEntity == null ) {
            throw new CustomException( "无此税收制度！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTaxRateSystemEntity>().
                set ( KingBaseTaxRateSystemEntity :: getIsDisabled, kingBaseTaxRateSystemEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseTaxRateSystemEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseTaxRateSystemEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseTaxRateSystemEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( kingBaseTaxRateSystemEntity.getIsDisabled () == 1 ? "禁用税收制度失败！" : "解禁税收制度失败" );
        }
        return AjaxResult.success ();
    }
}
